﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using System.IO;

public partial class Report_ViewPlanForMonthWeek : System.Web.UI.Page
{
    MonthPlanManager dllMonthPlanManager = new MonthPlanManager();
    WeekPlanofManager dllWeekPlanofManager = new WeekPlanofManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGridView();
        }
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-3-30</date>
    void bindGridView()
    {
        //月周计划类别月1周2
        int _planType = Utility.HttpHelp.getParamsInt("PlanType");
        //部门类别,1是部门，0是中心
        string DepartmentType = Utility.HttpHelp.getParamsString("DepartmentType");
        //部门名使用in子句
        string DepartmentName = Utility.HttpHelp.getParamsString("DepartmentName");
        string DepartmentNames = string.Empty;
        //是否重点计划
        string isEmphasis = Utility.HttpHelp.getParamsString("isEmphasis");
        //计划状态，all 及数字 拼凑in 子句
        string state = Utility.HttpHelp.getParamsString("state");
        if (state == "all")
        {
            state = "1";
        }
        //起始周期
        int StartDate = Utility.HttpHelp.getParamsInt("StartDate");
        //截止周期
        int EndDate = Utility.HttpHelp.getParamsInt("EndDate");
        //如果是中心 ，获取部门字符串
        if (DepartmentType == "0")
        {
            DataTable dt_department = new BLL.DepartmentInfoManager().getDepartmentByCenter(new object[] { DepartmentName });
            foreach (DataRow dr in dt_department.Rows)
            {
                DepartmentNames += "'" + dr["DepartmentName"].ToString() + "',";
            }
            if (DepartmentNames.Length > 0)
            {
                DepartmentNames = DepartmentNames.Substring(0, DepartmentNames.Length - 1);
            }
        }
        else
        {
            DepartmentNames = "'" + DepartmentName + "'";
        }

        if (_planType == 1)
        {
            string MonthDate = string.Empty;
            //获取周期区间
            DataTable dt = new BLL.MonthDateManager().getMonthDateByStartDateEndDate(new object[] { StartDate, EndDate });
            foreach (DataRow dr in dt.Rows)
            {
                MonthDate += dr["Month_Id"] + ",";
            }

            if (MonthDate.Length > 0)
            {
                MonthDate = MonthDate.Substring(0, MonthDate.Length - 1);
            }

            object[] _value = new object[4];
            _value[0] = MonthDate;
            _value[1] = DepartmentNames;
            _value[2] = state;
            _value[3] = isEmphasis;
            dt = new BLL.MonthPlanManager().getMonthPlanByReport(_value);

            int i = 1;
            int index = 0;
            string Predepartment = string.Empty;
            string department = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                if (i == 1)
                {
                    Predepartment = department = dr["MasterDepartment"].ToString();
                }
                else
                {
                    department = dr["MasterDepartment"].ToString();
                    Predepartment = dt.Rows[index - 1]["MasterDepartment"].ToString();
                }
                if (Predepartment == department)
                {
                    dr["MonthPlanSortId"] = i;
                }
                else
                {
                    i = 1;
                    dr["MonthPlanSortId"] = i;
                }
                i++;
                index++;
            }
            this.gdvMonthPlanView.DataSource = dt;
            this.gdvMonthPlanView.DataBind();
            this.gdvMonthPlanView.Visible = true;

            #region 下载文件
            //for (int i = 0; i < this.gdvMonthPlanView.Rows.Count; i++)
            //{
            //    wrapByComma(gdvMonthPlanView, i, 7);
            //    if (this.gdvMonthPlanView.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            //    {
            //        Button _btn = (Button)this.gdvMonthPlanView.Rows[i].Cells[13].FindControl("btnDownLoad");
            //        _btn.Visible = false;
            //        Label _lbl = (Label)this.gdvMonthPlanView.Rows[i].Cells[13].FindControl("lblAlert");
            //        _lbl.Visible = true;
            //        _lbl.Text = "无附件";
            //    }



            //    if (this.gdvMonthPlanView.Rows[i].Cells[11].Text == "&nbsp;" || this.gdvMonthPlanView.Rows[i].Cells[11].Text == "&amp;nbsp;")
            //    {
            //        this.gdvMonthPlanView.Rows[i].Cells[11].Text = "";
            //    }

            //}
            #endregion
        }
        if (_planType == 2)
        {
            string WeekDate = string.Empty;
            //获取周期区间
            DataTable dt = new BLL.WeekDateManager().getWeekDateByStartDateEndDate(new object[] { StartDate, EndDate });
            foreach (DataRow dr in dt.Rows)
            {
                WeekDate += dr["Week_Id"] + ",";
            }

            if (WeekDate.Length > 0)
            {
                WeekDate = WeekDate.Substring(0, WeekDate.Length - 1);
            }

            object[] _value = new object[4];
            _value[0] = WeekDate;
            _value[1] = DepartmentNames;
            _value[2] = state;
            _value[3] = isEmphasis;
            dt = new BLL.WeekPlanofManager().getWeekPlanByReport(_value);
            int i = 1;
            int index = 0;
            string Predepartment = string.Empty;
            string department = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                if (i == 1)
                {
                    Predepartment = department = dr["MasterDepartment"].ToString();
                }
                else
                {
                    department = dr["MasterDepartment"].ToString();
                    Predepartment = dt.Rows[index - 1]["MasterDepartment"].ToString();
                }
                if (Predepartment == department)
                {
                    dr["WeekPlanSortId"] = i;
                }
                else
                {
                    i = 1;
                    dr["WeekPlanSortId"] = i;
                }
                i++;
                index++;
            }
            this.gdvWeekPlanView.DataSource = dt;
            this.gdvWeekPlanView.DataBind();
            this.gdvWeekPlanView.Visible = true;

            #region 下载文件
            //for (int i = 0; i < this.gdvWeekPlanView.Rows.Count; i++)
            //{
            //    wrapByComma(gdvWeekPlanView, i, 7);
            //    if (this.gdvWeekPlanView.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            //    {
            //        Button _btn = (Button)this.gdvWeekPlanView.Rows[i].Cells[13].FindControl("btnDownLoad");
            //        _btn.Visible = false;
            //        Label _lbl = (Label)this.gdvWeekPlanView.Rows[i].Cells[13].FindControl("lblAlert");
            //        _lbl.Visible = true;
            //        _lbl.Text = "无附件";
            //    }


            //    if (this.gdvWeekPlanView.Rows[i].Cells[11].Text == "&nbsp;" || this.gdvWeekPlanView.Rows[i].Cells[11].Text == "&amp;nbsp;")
            //    {
            //        this.gdvWeekPlanView.Rows[i].Cells[11].Text = "";
            //    }

            //}
            #endregion
        }




        //for (int i = 0; i < this.gdvMonthPlanView.Rows.Count; i++)
        //{
        //    if (this.gdvMonthPlanView.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
        //    {
        //        Button _btn = (Button)this.gdvMonthPlanView.Rows[i].Cells[12].FindControl("btnDownLoad");
        //        _btn.Visible = false;
        //        Label _lbl = (Label)this.gdvMonthPlanView.Rows[i].Cells[12].FindControl("lblAlert");
        //        _lbl.Visible = true;
        //        _lbl.Text = "无下载";
        //    }
        //    if (this.gdvMonthPlanView.Rows[i].Cells[11].Text == "&nbsp;" || this.gdvMonthPlanView.Rows[i].Cells[11].Text == "&amp;nbsp;" || this.gdvMonthPlanView.Rows[i].Cells[11].Text == "&amp;")
        //    {
        //        this.gdvMonthPlanView.Rows[i].Cells[11].Text = "";
        //    }



        //}
        //for (int i = 0; i < this.gdvWeekPlanView.Rows.Count; i++)
        //{
        //    if (this.gdvWeekPlanView.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
        //    {
        //        Button _btn = (Button)this.gdvWeekPlanView.Rows[i].Cells[12].FindControl("btnDownLoad");
        //        _btn.Visible = false;
        //        Label _lbl = (Label)this.gdvWeekPlanView.Rows[i].Cells[12].FindControl("lblAlert");
        //        _lbl.Visible = true;
        //        _lbl.Text = "无下载";
        //    }
        //    if (this.gdvWeekPlanView.Rows[i].Cells[11].Text == "&nbsp;" || this.gdvWeekPlanView.Rows[i].Cells[11].Text == "&amp;nbsp;" || this.gdvWeekPlanView.Rows[i].Cells[11].Text == "&amp;")
        //    {
        //        this.gdvWeekPlanView.Rows[i].Cells[11].Text = "";
        //    }
        //}
    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        //string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        ////_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }
    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }

    /// <summary>
    /// 月附件下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnMonthDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvMonthPlanView.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }

    /// <summary>
    /// 周下载附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnWeekDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvWeekPlanView.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }

    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[2];
        _gridView[0] = this.gdvMonthPlanView;        
        _gridView[1] = this.gdvWeekPlanView;
        Utility.Comm.ViewForMonthWeekToExcel(GridViewToDataTable(_gridView));
    }

    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < _sourceGridView[i].Columns.Count; j++)
            {
                if (_sourceGridView[i].Columns[j].Visible == true)
                {
                    dc = new DataColumn(_sourceGridView[i].Columns[j].HeaderText);
                    _targetDataTable[i].Columns.Add(dc);
                }
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < _sourceGridView[i].Columns.Count; k++)
                {

                    if (_sourceGridView[i].Columns[k].Visible == true && k < _targetDataTable[i].Columns.Count)
                    {
                        if (_sourceGridView[i].Columns[k].HeaderText == "计划来源")
                        {
                            LinkButton lbtn = (LinkButton)_sourceGridView[i].Rows[j].Cells[k].FindControl("lbtnSource");
                            dr[k] = lbtn.Text;
                        }
                        else
                        {
                            dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;
                        }

                    }

                }
                _targetDataTable[i].Rows.Add(dr);
            }

        }
        return _targetDataTable;
    }
}